package com.accompany.controller;

import com.accompany.common.Result;
import com.accompany.entity.Orders;
import com.accompany.service.OrdersService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 订单表 前端控制器
 * </p>
 *
 * @author DC
 * @since 2024-12-26
 */
@RestController
@RequestMapping("/orders")
@Tag(name = "OrdersController", description = "订单管理")
public class OrdersController {

    @Autowired
    private OrdersService orderService;

    /**
     * 创建订单
     *
     * @param order 订单信息
     * @return Result对象，包含操作结果
     */
    @PostMapping("/create")
    @Operation(summary = "创建订单", description = "创建一个新的订单")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "订单创建成功"),
            @ApiResponse(responseCode = "400", description = "请求参数不正确")
    })
    public Result<?> createOrder(@RequestBody Orders order) {
        orderService.createOrder(order);
        return Result.success();
    }

    /**
     * 删除订单
     *
     * @param id 订单ID
     * @return Result对象，包含操作结果
     */
    @DeleteMapping("/delete/{id}")
    @Operation(summary = "删除订单", description = "根据订单ID删除订单")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "订单删除成功"),
            @ApiResponse(responseCode = "404", description = "订单不存在")
    })
    public Result<?> deleteOrder(@PathVariable Long id) {
        orderService.deleteOrder(id);
        return Result.success("删除成功");
    }

    /**
     * 更新订单信息
     *
     * @param order 订单信息
     * @return Result对象，包含操作结果
     */
    @PutMapping("/update")
    @Operation(summary = "更新订单信息", description = "根据订单ID更新订单信息")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "订单信息更新成功"),
            @ApiResponse(responseCode = "404", description = "订单不存在")
    })
    public Result<?> updateOrder(@RequestBody Orders order) {
        orderService.updateOrder(order);
        return Result.success("更新成功");
    }

    /**
     * 获取单个订单信息
     *
     * @param id 订单ID
     * @return Result对象，包含订单信息
     */
    @GetMapping("/{id}")
    @Operation(summary = "获取单个订单信息", description = "根据订单ID获取订单信息")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "获取成功"),
            @ApiResponse(responseCode = "404", description = "订单不存在")
    })
    public Result<Orders> getOrderById(@PathVariable Long id) {
        Orders order = orderService.getOrderById(id);
        return Result.success(order);
    }

    /**
     * 获取所有订单列表
     *
     * @return Result对象，包含订单列表
     */
    @GetMapping
    @Operation(summary = "获取所有订单列表", description = "获取所有订单的列表")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "获取成功"),
            @ApiResponse(responseCode = "500", description = "服务器内部错误")
    })
    public Result<List<Orders>> getAllOrders() {
        List<Orders> orders = orderService.getAllOrders();
        return Result.success(orders);
    }

    /**
     * 分页查询订单列表
     *
     * @param page  页码
     * @param size  每页大小
     * @return Result对象，包含分页后的订单列表
     */
    @GetMapping("/page")
    @Operation(summary = "分页查询订单列表", description = "根据页码和每页大小分页查询订单列表")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "分页查询成功"),
            @ApiResponse(responseCode = "500", description = "服务器内部错误")
    })
    public Result<Page<Orders>> getPaginatedOrders(
            @Parameter(description = "页码", required = true) @RequestParam(name = "page", defaultValue = "1") Integer page,
            @Parameter(description = "每页大小", required = true) @RequestParam(name = "size", defaultValue = "10") Integer size) {
        Page<Orders> pageResult = orderService.getPaginatedOrders(page, size);
        return Result.success(pageResult);
    }
}